package com.sskj.api.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sskj.api.domain.SysUser;
import com.sskj.api.domain.Role;
import com.sskj.api.service.IRolePermissionService;
import com.sskj.api.service.IRoleService;
import com.sskj.api.service.IUserRoleService;
import com.sskj.api.vo.req.GrantPermissionReq;
import com.sskj.api.vo.req.RoleBindUserIdsRep;
import com.sskj.api.vo.req.UserBindRolesReq;
import com.sskj.api.vo.req.RolePageListReq;
import com.sskj.common.util.ApiResponseUtil;
import com.sskj.common.vo.ApiResponse;
import com.sskj.common.vo.SimplePagination;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "角色模块")
@RequestMapping("/admin/role")
@Validated
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IRolePermissionService rolePermissionService;

    @Operation(summary = "新增角色")
    @PostMapping("add")
    public ApiResponse<Object> add(Role role) {
        roleService.add(role);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "删除角色")
    @PostMapping("deleteById")
    public ApiResponse<Object> deleteById(@RequestParam @NotNull Long id) {
        roleService.deleteById(id);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新角色")
    @PostMapping("update")
    public ApiResponse<Object> update(@RequestBody Role role) {
        roleService.update(role);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "分页列表")
    @GetMapping("pageList")
    public ApiResponse<SimplePagination<Role>> pageList(RolePageListReq req) {
        IPage<Role> roleIPage = roleService.pageList(req);

        return ApiResponseUtil.success(roleIPage);
    }

    @Operation(summary = "用户绑定多角色")
    @PostMapping("bindRoles")
    public ApiResponse<Object> bindRoles(@RequestBody @Valid UserBindRolesReq req) {
        userRoleService.bindRoles(req.getUserId(), req.getRoleIds());

        return ApiResponseUtil.success();
    }

    @Operation(summary = "角色绑定多用户")
    @PostMapping("bindUserIds")
    public ApiResponse<Object> bindUserIds(@RequestBody @Valid RoleBindUserIdsRep rep) {
        userRoleService.bindUserIds(rep.getRoleId(), rep.getUserIds());

        return ApiResponseUtil.success();
    }

    @Operation(summary = "用户绑定角色列表")
    @GetMapping("listByUserId")
    public ApiResponse<List<Role>> listByUserId(@RequestParam Long userId) {
        List<Role> roles = userRoleService.listByUserId(userId);

        return ApiResponseUtil.success(roles);
    }

    @Operation(summary = "角色绑定的用户分页列表")
    @GetMapping("adminPageListByRoleId")
    public ApiResponse<SimplePagination<SysUser>> adminPageListByRoleId(@RequestParam Long roleId,
                                                                        @RequestParam(name = "pageNum", defaultValue = "1") Long pageNum,
                                                                        @RequestParam(name = "pageSize", defaultValue = "10") Long pageSize) {
        IPage<SysUser> adminUserIPage = userRoleService.pageListByRoleId(roleId, pageNum, pageSize);

        return ApiResponseUtil.success(adminUserIPage);
    }

    @Operation(summary = "解绑关系")
    public ApiResponse<Object> unBind(@RequestParam Long userRoleId) {
        userRoleService.unBind(userRoleId);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "分配权限")
    @PostMapping("grantPermission")
    public ApiResponse<Object> grantPermission(@Valid @RequestBody GrantPermissionReq grantPermissionReq) {
        rolePermissionService.saveOrUpdate(grantPermissionReq.getRoleId(), grantPermissionReq.getPermissionIds());

        return ApiResponseUtil.success();
    }
}
